#the_post
Explore tagged Tumblr posts
Text
wordpress 特定の記事IDを指定してアイキャッチを表示
<?php $query_feature = new WP_Query( array('post_type' => 'postとかpageとかカスタム投稿の名前featureとか') ); ?> <?php if ($query_feature->have_posts()): $query_feature->the_post(); ?>
<?php $feature_post_id = 1523; ?>// ←指定記事のID <a class="funcItem" href="<?php echo get_permalink($feature_post_id); ?>"> <div class="funcItem__img"><?php echo get_the_post_thumbnail($feature_post_id, 'full'); ?></div> <h3 class="funcItem__title"><?php echo get_post($feature_post_id )->post_title; ?></h3> </a> <?php endif; wp_reset_postdata(); ?>
備忘録。
0 notes
Text
Customizing WooCommerce Product Pages for Maximum Impact
Success in e-commerce is dependent not only on the quality of your products but also on how you offer them to your buyers. Your online store's product pages are digital shops that can dramatically impact your visitors' shopping decisions. Customizing these pages successfully may make a huge difference, and in this blog, we'll look at how to do just that with WooCommerce, one of the most popular WordPress e-commerce platforms.
WooCommerce provides a powerful set of customization options, allowing you to personalize your product pages to the specific demands and aesthetics of your brand. In this article, we'll go over several technical features, along with code samples, to help you improve the effect of your WooCommerce product pages. Whether you're a seasoned developer or new to WordPress and WooCommerce, you'll find useful tips and code examples to improve your e-commerce site's product pages. Let's get this party started!
Understanding WooCommerce's Template Structure
Before we begin customizing WooCommerce, we must first grasp the template structure. WooCommerce uses templates to display different aspects of your e-commerce site, such as product pages. These templates are kept in the folder of your WordPress theme, allowing you to override and alter them without having to modify the main WooCommerce code.
To begin customizing, navigate to the directory of your active theme and create a new folder called woocommerce if it does not already exist. Custom templates will be placed here to alter WooCommerce's default functionality.
Customizing WooCommerce Product Page Layout
Let's start with something basic: adjusting the product page layout. You might want to move the product image to a different location or add extra information to the product description area. To do this, you can create a custom single-product.php template in your theme's woocommerce folder.
<?php
//WooCommerce Product Page Template
get_header();
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
wc_get_template_part( 'content', 'single-product' );
}
}
get_footer();
?> In the above code snippet, we're creating a custom single-product.php template, which will be used to display individual product pages. You can modify this template as per your requirements, moving or adding elements to change the page layout.
Adding Custom Fields and Information
You may need to display more product information in addition to what WooCommerce provides by default. For example, you might wish to display product details or user reviews from other websites. You may accomplish this by utilizing custom fields and hooks.
Here's an example of how to add a custom field for product specifications:
// Add custom field for product specifications
function add_product_specifications() {
$product_specifications = get_post_meta( get_the_ID(), 'product_specifications', true );
if ( $product_specifications ) {
echo '<div class="product-specifications">';
echo '<h2>Specifications</h2>';
echo '<p>' . esc_html( $product_specifications ) . '</p>';
echo '</div>';
}
}
add_action( 'woocommerce_single_product_summary', 'add_product_specifications', 25 );
In this code snippet, we're hooking into the woocommerce_single_product_summary action to display custom product specifications. You can adapt this code to add other custom information or fields as needed.
Customizing WooCommerce product pages can have a big impact on the user experience and conversion rates of your shop. You have the ability to develop product pages that exactly correspond with your brand's identity and fulfill your consumers' expectations by understanding the template structure and employing code snippets like the ones supplied above. Keep an eye out for further technical tips and tricks for optimizing your WooCommerce-powered e-commerce site. To know more visit us at https://magnigeeks.com/
0 notes
Photo
... کرونا و قرنطینه باعث شد فیلمهای خوبی از کارهای مرد بزرگ عالم سینما، استیون اسپیلبرگ رو ببینم و به شما دوستان توصیه میکنم بعضی از این فیلمها رو حتما ببینید، چون واقعا شاهکارن. 1-Saving Private Ryan: فیلم «نجات سرباز رایان» ساخته ۱۹۹۸ در ژانر جنگ جهانی، با بازی خارقالعاده تام هَنکس، و فیلم برداری و موسیقی خاص خود مخاطب را شدیداً در فضای جنگ قرار میدهد. توصیه اکید میکنم برای تماشای این اثر ماندگار. 2-Schindler's List: فیلم «لیست شیندلر» ساخته ۱۹۹۳ ، یک درام تاریخی بسیار هیجان انگیز مربوط به شکنجه یهودیان توسط ارتش نازی است، اسپیلبرگ با تصمیم خود این فیلم را سیاه سفید کرده و فقط در کل فیلم یک صحنه رنگی دارد، که اگر فیلم را دیده باشید کار هوشمندانه او را تحسین میکنید. 3-The Post: فیلم « پُست » ساخته ۲۰۱۷ ، اثری تاریخی سیاسی است، که به اسناد تاریخی محرمانه جنگ ایالات متحده با ویتنام پرداخته است، فضای فیلم ژورنالیستی است و رقابت بین واشنگتن پست و رسانه ها و سیاسیون ��مریکا را به تصویر کشیده است. توصیه نمیکنم. 4-Catch me if You can: فیلم « اگه میتونی منو بگیر » ، ۲۰۰۲ ، با بازی لئوناردو دیکاپریو و تام هنکس در ژانر جنایی بیوگرافی است، هیجان بالای فیلم مخاطب را به وجد می آورد. اما چه چیزی باعث هیجان در این فیلم می شود؟ مطمئنم که دیدید. 5-Jurassic park: «پارک ژوراسیک» ، ۱۹۹۳ ، علمی تخیلی، تفکرات یک سرمایه گذار طماع که علاقه زیادی به تأسیس پارکی برای نگه داری دایناسور هاست را به نمایش میگذارد، وقتی به سال تولید فیلم نگاه میکنید میفهمید که در آن زمان چه شاهکاری بوده است.توصیه میکنم ببینید. 6-The lost World: Jurassic: این فیلم هم ادامه پارک ژوراسیک است که در سال ۱۹۹۷ تولید شد. 7-Bridge of Spies: «پل جاسوسها», ۲۰۱۵, یک درام تاریخی در مورد جنگ سرد ایالات متحده و شوروی است و جاسوس هایی که در مورد مسائل اتمی در کشورهای مقابل فعالیت میکنند. فضاسازی فیلم شگفت انگیز است مخصوصا صحنه های روی پل و سفارت شوروی. 🔺امیدوارم از دیدن این فیلمها لذت ببرید. #stevenspielberg #tomhanks #leonardodicaprio #saving_private_ryan #schindlerslist #the_post #catchmeifyoucan #jurassicpark #bridgeofspies #استیون_اسپیلبرگ #تام_هنکس #ليوناردو_دي_كابريو #نجات_سرباز_رایان #لیست_شیندلر #اگه_میتونی_منو_بگیر #پارک_ژوراسیک #پل_جاسوسان https://www.instagram.com/p/CAJNTvJJ8-f/?igshid=b8sb1lk91fdn
#stevenspielberg#tomhanks#leonardodicaprio#saving_private_ryan#schindlerslist#the_post#catchmeifyoucan#jurassicpark#bridgeofspies#استیون_اسپیلبرگ#تام_هنکس#ليوناردو_دي_كابريو#نجات_سرباز_رایان#لیست_شیندلر#اگه_میتونی_منو_بگیر#پارک_ژوراسیک#پل_جاسوسان
1 note
·
View note
Photo
[photoshop#1] whatt???. . #miawaug #apiip #pewdiepie #jacksepticeye #gamer #whatt #minecraft #enderman #shock #sub2pewdie #septicbooper #photoshop #2k19 . 19w38a (at Out of Nowhere.)
#1#miawaug#apiip#pewdiepie#jacksepticeye#gamer#whatt#minecraft#enderman#shock#sub2pewdie#septicbooper#photoshop#2k19#the_post
1 note
·
View note
Text
Wordpress: redirect delle pagine archivio con singolo risultato
Se vi siete imbattuti in questo articolo è probabile che possediate un blog Wordpress e che siate alla ricerca di un modo per reindirizzare tramite HTTP REDIRECT le URL delle vostre pagine archivio che contengono un singolo articolo. Le pagine archivio, in Wordpress, sono quelle pagine che vengono auto-generate quando l'utente fa click su una categoria o un tag, presentando un elenco paginato di tutti gli articoli che appartengono alla categoria slezionata o che contengono quel determinato tag. Si tratta di una funzionalità molto utile per consentire all'utente di recuperare gli articoli correlati, specialmente quando la "ricerca" restituisce un gran numero di risultati; al tempo stesso, in tutti i casi in cui la pagina archivio contiene un singolo articolo, potrebbe essere più efficiente - sia in ottica SEO che in termini di user-experience - evitare di mostrare la pagina di elenco e reindirizizzare l'utente direttamente al singolo articolo risultante. Se desiderate implementare questa funzionalità sul vostro blog potete aggiungere il seguente snippet di codice al vostro file function.php: /// RYADEL ADDED: 302-redirect single-post archives (tags or categories) to the relevant post function redirect_single_post_archives_to_post(){ global $wp_query; if (is_archive() && $wp_query->post_count == 1 ){ the_post(); $post_url = get_permalink(); wp_redirect($post_url, 302); } } add_action('template_redirect', 'redirect_single_post_archives_to_post'); Per effettuare questa operazione è sufficiente accedere all'interfaccia di amministrazione di Wordpress e navigare all'interno delle sezioni Aspetto > Editor: una volta lì, potrete selezionare il file function.php dall'elenco mostrato a destra. Per il momento è tutto: spero che questo articolo possa rivelarsi utile a chiunque abbia interesse a implementare questa funzionalità sul proprio sito Wordpress! Read the full article
1 note
·
View note
Text
The Loop <?php if ( have_posts() ) { while ( have_posts() ) { the_post(); <!-- Test if the current post is in category 3. --> <!-- If it is, the div box is given the CSS class "post-cat-three". --> <!-- Otherwise, the div box is given the CSS class "post". --> <?php if ( in_category( '3' ) ) : ?> <div class="post-cat-three"> <?php else : ?> <div class="post"> <?php endif; ?> <!-- Display the…
View On WordPress
0 notes
Text
【WP】whileループのn番目毎に要素を追加する
% 6 のとこがn番目。 六番目の要素の次に $thumb_item が挿入される。
<?php if ( $the_query->have_posts() ): ?> <div class="thumbs_list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $count = $the_query->current_post; if (0 === ($the_query->current_post) % 6 && !$count == 0):
$thumb_item = <<<EOM // <p>要素</p> EOM; // 「EOM;」のインデントは絶対一番端にしなきゃ効かない
echo $thumb_item;
endif; get_template_part( 'template-parts/content', 'thumb' ); endwhile; ?>
</div> <?php endif; ?>
0 notes
Text
基本のWordpress関数と今すぐ使えるページ毎の条件分岐
基本のWordpress関数と今すぐ使えるページ毎の条件分岐 #WordPress の勉強をなにからしたらいいか迷っている方に初歩から役立つ情報をお届けします。オリジナルテーマを作成するのに必要な基本の#WordPress関数 や今すぐ使える#ページ毎の条件分岐 についてご紹介いたします。 #alfactory #アルファクトリー 続きはWEBで
WordPressの勉強をなにからしたらいいか迷っている方に初歩から役立つ情報をお届けします。オリジナルテーマを作成するのに必要な基本のWordPress関数や今すぐ使えるページ毎の条件分岐についてご紹介いたします。 WordPress関数 WordPressにはあらかじめ動的サイトの作成に便利な関数がたくさん定義(登録)されており、これらを使いこなすことがオリジナルテーマの作成に必要なスキルとなってきます。WordPress関数を覚えることはスキルアップに繋がり、できることの幅が広がるので覚えるのは大変ですが、ぜひ覚えて理解しましょう。 代表的なWordPress関数 記事にまつわる情報を出力・取得 the_post() ループ内に配置・必須。記事に関するさまざまな情報を取得してくれます。ループ内にこれがないと記事に紐づく情報が取得できません。 the_title() ループ内で利…
View On WordPress
0 notes
Photo
... کرونا و قرنطینه باعث شد فیلمهای خوبی از کارهای مرد بزرگ عالم سینما، استیون اسپیلبرگ رو ببینم و به شما دوستان توصیه میکنم بعضی از این فیلمها رو حتما ببینید، چون واقعا شاهکارن. 1-Saving Private Ryan: فیلم «نجات سرباز رایان» ساخته ۱۹۹۸ در ژانر جنگ جهانی، با بازی خارقالعاده تام هَنکس، و فیلم برداری و موسیقی خاص خود مخاطب را شدیداً در فضای جنگ قرار میدهد. توصیه اکید میکنم برای تماشای این اثر ماندگار. 2-Schindler's List: فیلم «لیست شیندلر» ساخته ۱۹۹۳ ، یک درام تاریخی بسیار هیجان انگیز مربوط به شکنجه یهودیان توسط ارتش نازی است، اسپیلبرگ با تصمیم خود این فیلم را سیاه سفید کرده و فقط در کل فیلم یک صحنه رنگی دارد، که اگر فیلم را دیده باشید کار هوشمندانه او را تحسین میکنید. 3-The Post: فیلم « پُست » ساخته ۲۰۱۷ ، اثری تاریخی سیاسی است، که به اسناد تاریخی محرمانه جنگ ایالات متحده با ویتنام پرداخته است، فضای فیلم ژورنالیستی است و رقابت بین واشنگتن پست و رسانه ها و سیاسیون آمریکا را به تصویر کشیده است. توصیه نمیکنم. 4-Catch me if You can: فیلم « اگه میتونی منو بگیر » با بازی لئوناردو دیکاپریو و تام هنکس در ژانر جنایی بیوگرافی است، هیجان بالای فیلم مخاطب را به وجد می آورد. اما چه چیزی باعث هیجان در این فیلم می شود؟ مطمئنم که دیدید. 5-Jurassic park: «پارک ژوراسیک» ، ۱۹۹۳ ، علمی تخیلی، تفکرات یک سرمایه گذار طماع که علاقه زیادی به تأسیس پارکی برای نگه داری دایناسور هاست را به نمایش میگذارد، وقتی به سال تولید فیلم نگاه میکنید میفهمید که در آن زمان چه شاهکاری بوده است.توصیه میکنم ببینید. 6-The lost World: Jurassic: این فیلم هم ادامه پارک ژوراسیک است . 7-Bridge of Spies: «پل جاسوسها» یک درام تاریخی در مورد جنگ سرد ایالات متحده و شوروی است و جاسوس هایی که در مورد مسائل اتمی در کشورهای مقابل فعالیت میکنند. فضاسازی فیلم شگفت انگیز است مخصوصا صحنه های روی پل و سفارت شوروی. 🔺امیدوارم از دیدن این فیلمها لذت ببرید. #stevenspielberg #tomhanks #leonardodicaprio #saving_private_ryan #schindlerslist #the_post #catchmeifyoucan #jurassicpark #bridgeofspies #استیون_اسپیلبرگ #تام_هنکس #ليوناردو_دي_كابريو #نجات_سرباز_رایان #لیست_شیندلر #اگه_میتونی_منو_بگیر #پارک_ژوراسیک #پل_جاسوسان https://www.instagram.com/p/CAJNTvJJ8-f/?igshid=gla92ob9df7n
#stevenspielberg#tomhanks#leonardodicaprio#saving_private_ryan#schindlerslist#the_post#catchmeifyoucan#jurassicpark#bridgeofspies#استیون_اسپیلبرگ#تام_هنکس#ليوناردو_دي_كابريو#نجات_سرباز_رایان#لیست_شیندلر#اگه_میتونی_منو_بگیر#پارک_ژوراسیک#پل_جاسوسان
0 notes
Text
keep recommended
this video is still show up until now.. idk what jacksepticeye thought about this.. but, i love it anyway 😄😄
0 notes
Photo
WP_Query Arguments: Categories and Tags
In the earlier parts of this series, you've learned how WP_Query is structured and what its properties and methods are. The next stage is to understand the various arguments you can use with it and how best to do so.
WP_Query has a large number of possible arguments, which makes it extremely flexible. As you can use it to query just about anything held in your wp_posts table, it has arguments for every permutation of query you might want to run on your content.
In this tutorial I'll look at two types of argument for categories and tags:
The arguments for these two taxonomies are similar but do have some differences you need to know about if you're going to use them effectively.
A Recap on How Arguments Work in WP_Query
Before we start, let's have a quick recap on how arguments work in WP_Query. When you code WP_Query in your themes or plugins, you need to include four main elements:
the arguments for the query, using parameters which will be covered in this tutorial
the query itself
the loop
finishing off: resetting post data
In practice this will look something like the following:
<?php $args = array( // Arguments for your query. ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); ?>
The arguments are what tells WordPress what data to fetch from the database and it's those that I'll cover here. So all we're focusing on here is the first part of the code:
$args = array( // Arguments for your query. );
As you can see, the arguments are contained in an array. You'll learn how to code them as you work through this tutorial.
Coding Your Arguments
There is a specific way to code the arguments in the array, which is as follows:
$args = array( 'parameter1' => 'value', 'parameter2' => 'value', 'parameter3' => 'value' );
You must enclose the parameters and their values in single quotation marks, use => between them, and separate them with a comma. If you get this wrong, WordPress may not add all of your arguments to the query or you may get a white screen.
Category Parameters
Let's start with category parameters. The options you have here are as follows:
cat (int): use category id.
category_name (string): use category slug (NOT name).
category__and (array): use category id.
category__in (array): use category id.
category__not_in (array): use category id.
Note that for none of these do you use the name of your category. Even the category_name parameter takes the slug as its value, not its name. I tend to use this one rather than the ID as when I come back to my code at a later date, slugs are easier to identify than IDs. However if you think your site users might change the slug for one or more categories, I recommend using the ID to avoid any problems.
Let's take a look at how you use each of these.
The cat Parameter
The cat parameter is straightforward: just use a single category ID or a string of category IDs.
Querying for one category looks like this:
$args = array( 'cat' => '12' );
Querying for multiple categories looks like this:
$args = array( 'cat' => '12, 13, 14' );
The above will tell WordPress to fetch posts that are in any of the categories listed. If you want to find posts in every one of an array of categories, you use the category_and parameter, of which more shortly.
You can also use the cat parameter to find posts that are in one category but not another, by using a minus sign before the category ID as follows:
$args = array( 'cat' => '12, -13' );
The above would query posts in category 12 but not in category 13.
The category_name Parameter
The category_name parameter uses the category slug, not the name (confusing, I know!). Again you can use it with a single category or with a string of categories to find posts that are in any of the categories.
To query posts in a single category you add:
$args = array( 'category_name' => 'my-slug' );
And to find posts in one or more of a number of categories, use this:
$args = array( 'category_name' => 'my-slug, your-slug, another-slug' );
As with the cat parameter, this won't find posts that are in all of the categories, but it will find posts in any of the categories.
The category__and Parameter
If you want to find posts that are in all of an array of categories, this is the parameter you use. It takes the category IDs for its value. So to find posts in all of three categories, you would use:
$args = array( 'category__and' => array( '12', '13', '14' ) );
Note that this uses an array not a string, so you code it differently. The parameter has two underscores in its name: use just one and it won't work.
The category__in Parameter
The next parameter looks for posts in one or more of an array of categories. It actually works in the same way as the cat parameter, and also takes the category ID as its value.
So to query posts in one or more of an array of categories, you would use:
$args = array( 'category__in' => array( '12', '13', '14' ) );
The above would fetch posts from one or more of these categories.
The category__not_in Parameter
The category__not_in parameter does as you would expect: it queries posts which are not in a category or an array of categories.
To exclude posts from one category, you would use the following:
$args = array( 'category__not_in' => '12' );
And to exclude posts from an array of categories:
$args = array( 'category__not_in' => array( '12', '13', '14' ) );
This would exclude posts from any of these categories.
Tag Parameters
Tags have slightly different parameters from categories: you can't work out what they might be based on your knowledge of category parameters, I'm afraid!
The tag parameters are:
tag (string): use tag slug.
tag_id (int): use tag id.
tag__and (array): use tag ids.
tag__in (array): use tag ids.
tag__not_in (array): use tag ids.
tag_slug__and (array): use tag slugs.
tag_slug__in (array): use tag slugs.
Let's look at each of these.
The tag Parameter
The tag parameter takes the tag slug for its value and can be used to find posts with one tag or with any of a string of tags.
So to find posts with one tag you use:
$args = array( 'tag' => 'my-tag' );
And to find posts with tags from an array of tags:
$args = array( 'tag' => 'my-tag, your-tag, another-tag' );
Note that the above queries posts with any of the tags in the array, not all of them.
The tag_id Parameter
The tag_id parameter works in a similar way to the cat parameter: it takes the tag ID and can be used with a single tag or multiple tags.
To find posts with a single tag you use this:
$args = array( 'tag_id' => '21' );
To find posts with one or more tags from a string of tag IDs:
$args = array( 'tag_id' => '21, 22, 23' );
You can also use tag_id to exclude tags, either when using it for single tags or multiple tags.
So to query posts except those with a given tag, you'd use:
$args = array( 'tag_id' => '-21' );
While to find posts with one of two tags but without another tag, you'd use this:
$args = array( 'tag_id' => '21, -22, 23' );
So the above will query posts with either or both of tags 21 or 23 but not tag 22.
The tag__in Parameter
This parameter lets you find posts with one or more of an array of tags. It works in the same way as tag when used with an array:
$args = array( 'tag_in' => array( '21', '22', '23' ) );
This will query posts with any or all of the tags listed. If you want to find posts with all of the tags, you use tag__and, which I'll cover in a moment.
The tag__not_in Parameter
The tag__not_in parameter lets you query posts which don't have a given tag or array of tags.
Use it like this to exclude one tag:
$args = array( 'tag__not_in' => array( '21' ) );
Note that you still need to use an array even though you're only using one tag. For more tags, use:
$args = array( 'tag__not_in' => array( '21', '22', '23' ) );
This will query posts that don't have any of the above tags.
The tag_slug__and and tag_slug__in Parameters
These two parameters behave in exactly the same way as the tag__and and tag__in parameters, except that you use that tag slug in your arrays instead of the tag ID.
So for example to find posts which have both of a pair of tags, you use tag__slug_in:
$args = array( 'tag_slug__in' => array( 'my-tag', 'your-tag', 'another-tag' ) );
This finds posts with any of these tags. You could also use the tag parameter with a string of tag slugs to achieve the same result.
To include posts with all of a set of tags, use tag_slug__and:
$args = array( 'tag_slug__and' => array( 'my-tag', 'your-tag', 'another-tag' ) );
Instead of querying posts with any of the tags, this only queries posts that have all of the tags.
Advanced Use of Categories and Tags with WP_Query
Let’s take a look at some advanced uses of category and tag arguments with WP_Query, based on questions raised in the comments to this article.
List Subcategories of a Category
To list the subcategories of a given category (with a link to the subcategory archives for each), you wouldn’t use WP_Query. Instead you’d use wp_list_categories(), which outputs a list of categories with links to their archives. To output the subcategories of a specific category, use the ‘child_of' argument.
Here’s the code:
<?php $args = array ( ‘child_of’ => 5 ); wp_list_categories( $args ); ?>
Substitute the 5 in the code above for the ID of the category whose subcategories you want to list.
Display The First Post from a Category in One Element Then More Posts in Another Element
This is a useful technique if you want to highlight the most recent post from a category, maybe including the excerpt and featured image for that post, and then show a list of the next few posts from the category.
You do this by using the 'posts_per_page' and 'offset' arguments.
The simplest way to do this is to write two separate queries. The first uses 'posts_per_page' => 1 to just fetch the first post. The second query uses'offset' => 1 to fetch posts after the first post, and also uses 'posts_per_page' to define how many posts to output.
Here’s the code:
<?php // first query - one post $args = array( 'cat' => 2, 'posts_per_page' => 1 ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Restore original post data. wp_reset_postdata(); // second query - three posts $args = array( 'cat' => 2, 'posts_per_page' => 3, 'offset' => 1 ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) { // Start looping over the query results. while ( $query->have_posts() ) { $query->the_post(); // Contents of the queried post results go here. } } // Rewind the query. rewind_posts(); ?>
An alternative method, which involves fewer calls to the database, is to write one query and then loop back through that query a second time, rewinding the query and using a variable to make sure the post output by the first loop isn’t repeated.
You can find more about using one query for multiple loops in our tutorial on the subject.
Display Posts From Multiple Categories
If you want to display posts that are in a given category and also in one or more of your other categories, you use the cat and category__in arguments. For each of these you use the category ID.
Imagine you want your posts to be category 1 and also in one or more of categories 2, 3 and 5.
Here are the arguments you would use for this:
$args = array( 'cat' => '1', 'category__in' => array ( '2', '3', '5' ) );
Summary
Querying your posts by category and/or tag is something there's a good chance you'll have occasion to do with WP_Query. By using the arguments above, and combining them where necessary, you can create powerful arguments to extract exactly the data you need from the database.
If you want to learn more about WP_Query, check out some of my other posts in the Mastering WP_Query series.
WordPress
Mastering WP_Query: Using the Loop
Rachel McCollin
WordPress
Mastering WP_Query: Actions and Filters
Barış Ünver
WordPress
WP_Query Arguments: Status, Order and Pagination
Rachel McCollin
WordPress
Mastering WP_Query: 10 Useful Examples
Barış Ünver
by Rachel McCollin via Envato Tuts+ Code https://ift.tt/3mnJJDK
0 notes
Text
WordPress4.5.1で特定のパーマリンクだとシングルページが見れなくなる問題と対策
https://432web.net/2020/05/32364/
WordPress4.5.1で特定のパーマリンクだとシングルページが見れなくなる問題と対策
WordPress4.5.1で当ブログに障害が発生してしまったので、対応の流れを備忘録として記載。
現象
シングルページを開こうとすると、その記事だけが含まれるアーカイブページが出てくる。
こんな感じになって、見れない。4.5.1はマイナーアップデートとして自動適用されたので面食らった。
原因
パーマリンクに投稿IDやスラッグが入っていないせいで、シングルページのURLと認識されなくなった。
当ブログはパーマリンクを「/%year%/%monthnum%/%day%/%hour%%minute%%second%/」つまり「/年/月/日/時分秒/」としていた。 理由は確か…記事と関連する数字かつ桁が不変だから、とかだったかな。結局そこにこだわる意味は無かったのだが。
こ��形式は、パーマリンクに秒までの完全な投稿時間を入れると、投稿IDやスラッグが無くてもシングルページと判定してくれる仕様を利用したものだった。 が、どうやらお情け的に存在していた仕様だったらしく、4.5.1でフッと廃止された模様(正確な経緯は知らない)。 その結果、シングルページではなくアーカイブと判定されるように。
応急処理
とにかくすぐ復旧するための対処。WPを4.5.1以前の、4.5などにダウングレードさせればいい。 手段は何でもいいが、筆者は以下の記事を参考に「WP Downgrade Specific Core Version」プラグインで下げた。 これならダウングレードさせつつ、プラグインが有効な間はアプデを止められるようだ。
【WordPress】バージョン5.4.1においてパーマリンクの設定により投稿ページが表示されない不具合が発生|HTMLてくてく道
もちろんこれは急場しのぎであり、公式の気が変わらない限り今後永遠にWPのアプデができない。抜本的な解決が必要になる。
解決策
パーマリンクを変えてリダイレクトをかけるか、従来のURLでシングルページを呼び出す処理を入れるかの二択になる。
mode-n | パーマリンク判定仕様変更による対処方法 (暫定)
上の記事だとシングルページを呼ぶ場合のコードが載っている。URL変えずに済むので、総合的には一番楽な解決法だと思う。筆者は試してないけど。
当ブログではパーマリンクを「/%year%/%monthnum%/%post_id%/」に変えてリダイレクトすることにした。 理由はこうすりゃよかったなあと前から思っていたのと、非推奨から禁止になったような構造を続けるのもあんま気分よくないため。気分がすべて。
リダイレクトする場合の処理は見つからなかったので自力で組むことに。functions.phpに以下のように書いた。
add_action('init', 'changePermalink'); function changePermalink() $url = $_SERVER['REQUEST_URI']; // URL取得 if (preg_match('!/([0-9]4,)/([0-9]2,)/([0-9]2,)/([0-9]2,)([0-9]2,)([0-9]2,)/!', $url, $postTime) !== 1) return; // 「/年/月/日/時分秒/」を含むURLじゃなかったら中止 $query = new WP_Query(array( // クエリー設定 'post_type' => 'post', // 投稿タイプ 'post_status' => 'publish', // 公開済みの記事 'date_query' => array( 'year' => $postTime[1], 'month' => $postTime[2], 'day' => $postTime[3], 'hour' => $postTime[4], 'minute' => $postTime[5], 'second' => $postTime[6], ), )); if ($query -> have_posts()) $query -> the_post(); header('HTTP/1.1 301 Moved Permanently'); header('Location:' . get_the_permalink()); // 検出した記事へ301リダイレクト exit; wp_reset_postdata();
上の参考記事のコードに乗っかって$wp_query->query_varsに入ってる値で解決しちゃおうと思ったが、なんか値が年月までしか取れなかったので断念。旧パーマリンクでは時分秒を/で区切ってなかったせいだろうか。 そのためpreg_matchでURLを自力解析することに。できれば取っ掛かりのIF文で正規表現なんてやりたくなかったが…。
あとはWP_Queryのdate_queryで完全な投稿時間を指定、記事を特定してリダイレクトさせるという流れ。 もうちょっと洗練できそうな気はするが、本当にユニークなURLであったのならばとりあえずこれで飛べるはず。
パーマリンクを変えることによってどうしても残るデメリットとして、SEO効果と、アクセス解析等の計測関連で別記事扱いになってしまう点がある。 SEOは301リダイレクトを信じる。趣味ブログだから多少落ちぶれてもまあ構わない。 計測は気にするかどうかによる、筆者としては、シェアボタンにつけてるシェア数が0になっちゃったのが気になるが…ぶっちゃけシェア数なんてどうでもいいしカウント自体消すか。
0 notes
Text
wordpress 自动设置特色图片
效率提升:如何让wordpress自动设置特色图片
带着问题的行动是最高效的!
wordpress和微信小程序都是图文排版结构,除了标题还需要配置一张特色图片。每次都要手动设置特色图片非常麻烦,一直在思考:为什么不能将第一张图片设置特色? 为了解决这个问题,就开始专注解决。
体验微信小程序的图文排版
代码实现自动设置为特色图片
网上有一种代码方案,修改function.php:自动将上传的第一张图片设置为特色图片(前提这张图片必须在媒体库中,如果文章没有上传到媒体库的图片,显示默认图片),代码如下:
function wpforce_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } else { set_post_thumbnail($post->ID, '2611'); } } } //end function add_action('the_post', 'wpforce_featured'); add_action('save_post', 'wpforce_featured'); add_action('draft_to_publish', 'wpforce_featured'); add_action('new_to_publish', 'wpforce_featured'); add_action('pending_to_publish', 'wpforce_featured'); add_action('future_to_publish', 'wpforce_featured');
2611为图片的ID,快速将代码部署到了wordpress上
文章编辑器选择
这个解决方案有一个���制条件:编辑器上传图片的时候需要将图片上传到媒体库中,对比一下目前的编辑器的优缺点:
wordpress自带的浏览器编辑器(兼容windows和Mac),由于上传图片很麻烦,安装了『粘贴图片即上传的 WordPress 编辑器增强插件』—Imagepaste,可是上传的文件在媒体库,不能自动设置为特色图片。这种情况,只能设置一张默认了的特色图片。
Blogo(仅支持Mac os)可以设置特色图片,他兼容文本编辑框和Markdown编辑框,编辑功能非常也好用,但是最大的痛点是,不能截图粘贴图片。 *『Mweb』(仅支持Mac os), 只支持Markdown语法的wordpress发布软件,由于可以粘贴图片,同时上传的图片可以进入媒体库,所以是一个非常方便的编辑器。
最终文本编辑器的选择方案
如果是windows的电脑,直接用wordpress自带的网页编辑器,正文内容中的图片使用粘贴复制的模式,特色图片设置为固定的图片
如果是mac电脑,那么直接使用『Mweb』,上传图片方便,还可以使用Markdown语法,将焦点聚焦到内容中,不花费太多精力进行排版。
到此为止,非常愉快将问题进行了解决,但是又出现了一个问题,自己投入时间做这个事情,而更重要的事情却没有做,没有完成的更重要的事情,在潜意识中就会形成『稀缺心态或者说焦虑感』,这个问题引申到行为系统另外一个重要话题《如何成长》,后面进一步进行探讨。
酷口网: https://www.weqoocu.com/2618.html
0 notes
Text
Wordpress: redirect single-result archive pages to the actual post
If you've stumbled upon this post it most likely means that you're running a Wordpress blog with a lot of archive pages (categories and/or tags) containing a single post. Archive pages in Wordpress are those automatically-generated pages that appear when the user clicks on a category or tag, showing a paged list of all the posts that contain that tag or are related to that category. These lists are useful when they contain a lot of posts; however, whenever there's only a single relevant post, it would arguably be better to directly bring the user to the pertaining article, skipping the listing page. All you have to do to enforce such behaviour is to add the following source code snippet to your Wordpress installation's function.php file: /// RYADEL ADDED: 302-redirect single-post archives (tags or categories) to the relevant post function redirect_single_post_archives_to_post(){ global $wp_query; if (is_archive() && $wp_query->post_count == 1 ){ the_post(); $post_url = get_permalink(); wp_redirect($post_url, 302); } } add_action('template_redirect', 'redirect_single_post_archives_to_post'); In order to do that, go to Wordpress Administration panel, then navigate through Appearance > Theme Editor: from there, select the function.php file from the list to the right. That's about it: I hope that this snippet will be useful for those who want to enforce such behaviour in their Wordpress website! Read the full article
0 notes
Text
Ultimate WP_Query Guide with Arguments - Code Briefly
https://codebriefly.com/ultimate-wp-query-guide-with-arguments/
Ultimate WP_Query Guide with Arguments
In this article, we will discuss the WordPress WP_query with Arguments. You can use this in the custom query when you require. Basic WP_Query Syntax $args = array( // List of arguments ); $custom_query = new WP_Query( $args ); // The Loop if ( $custom_query->have_posts() ) { while ( $custom_query->have_posts() ) { $custom_query->the_post(); // Do Stuff […]
0 notes