On my Wordpress homepage, the post is showing only one line of description. How do I add new lines?
I want to show more lines. So how can I do it?
Add below shared code in your theme function.php file:-
/**
* Filter the except length to 100 words.
*
* #param int $length Excerpt length.
* #return int (Maybe) modified excerpt length.
*/
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );
function wpdocs_custom_excerpt_length( $length ) {
return 100;
}
Related
I'm trying to remove date properties from Article schema generated by the Yoast SEO plugin.
In their developer docs the wpseo_schema_article filter is set as an example for manipulating with Article graph piece. However even with this type="application/ld+json":
<script type="application/ld+json">
{
"#context":"https://schema.org",
"#type":"Article",
"mainEntityOfPage":{
"#type":"WebPage",
"#id":"https://www.myproscooter.com/etwow-electric-scooters-review/"
},
"headline":"E-Twow Electric Scooters 2021 Review",
"image":{
"#type":"ImageObject",
"url":"https://www.myproscooter.com/wp-content/uploads/2020/12/elek-scoot.jpg",
"width":700,
"height":400
},
"datePublished":"2020-12-08T08:52:13",
"dateModified":"2021-01-12T11:30:10",
"author":{
"#type":"Person",
"name":"Jason"
},
"publisher":{
"#type":"Organization",
"name":"MyProScooter",
"logo":{
"#type":"ImageObject",
"url":"https://www.myproscooter.com/wp-content/uploads/2021/01/MPS-Logo-228x60.png"
}
}
}
</script>
When I try to access and manipulate data like this:
add_filter( 'wpseo_schema_article', 'remove_article_dates' );
function remove_article_dates( $data ) {
file_put_contents(WP_CONTENT_DIR.'/helper-seo.txt','DATA PRE FILTER: '.print_r($data,true),FILE_APPEND);
unset($data['datePublished']);
unset($data['dateModified']);
return $data;
}
Nothing gets logged into helper-seo.txt nor do dates get unset in the Article schema; as if the filter is ignored totally.
What's more confusing is that manipulation with dates in Webpage Schema works and is similar to the above:
add_filter( 'wpseo_schema_webpage', 'remove_webpage_dates');
function remove_webpage_dates( $data ) {
unset($data['datePublished']);
unset($data['dateModified']);
return $data;
}
The other stuff I've tried include:
add_filter( 'wpseo_schema_article_date_published', '__return_false' );
add_filter( 'wpseo_schema_article_date_modified', '__return_false' );
Which isn't reflecting into Article schema at all. How to remove these properties sucessfully?
i am using this code, it will work
add_filter ( 'wpseo_schema_webpage' , 'remove_breadcrumbs_property_from_webpage' , 11 , 1 ) ;
function remove_breadcrumbs_property_from_webpage( $data ) {
if (array_key_exists('datePublished', $data)) {
unset($data['datePublished']);
unset($data['dateModified']);
}
return $data;
}
Try this exact code. I'm sure it will work for you. This cleared my problems at least.
// Remove DatePublished
add_filter( 'wpseo_schema_graph_pieces', 'remove_datePublished_from_schema', 11, 2 );
add_filter( 'wpseo_schema_webpage', 'remove_datePublished_property_from_webpage', 11, 1 );
/**
* Removes the DatePublished graph pieces from the schema collector.
*
* #param array $pieces The current graph pieces.
* #param string $context The current context.
*
* #return array The remaining graph pieces.
*/
function remove_datePublished_from_schema( $pieces, $context ) {
return \array_filter( $pieces, function( $piece ) {
return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\datePublished;
} );
}
/**
* Removes the DatePublished property from the WebPage piece.
*
* #param array $data The WebPage's properties.
*
* #return array The modified WebPage properties.
*/
function remove_datePublished_property_from_webpage( $data ) {
if (array_key_exists('datePublished', $data)) {
unset($data['datePublished']);
}
return $data;
}
I already created a function that will filter products using URL parameters and display them on the /shop/ page. That works fine.
But, that only works if the products template is set on the WooCommerce /shop/ page.
In my case the requirement is to have the category list set as default template on the /shop/ page. That means I need to override that template and show the filtered product list, when I run a product query with URL parameters.
(eg. example.com/shop/?param1=foo¶m2=bar)
I tried following code (and some iterations), but I'm stuck and have no clue how to override the default template when a product filter is applied:
add_filter( 'woocommerce_locate_template', [$this, 'my_include_template_function'], 10, 3 );
public function my_change_template_function($template, $template_name, $template_path)
{
if (!empty($_GET['param1']) && is_shop() && $template_name == 'content-product_cat.php') {
return wc_get_template_part('content', 'product');
} else {
return $template;
}
}
So the source of the categories loop is added in to the loop start [via a filter][1]
add_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
and the woocommerce_maybe_short_product_subcategories() function changes the output based on the:
$display_type = woocommerce_get_loop_display_mode();
However, the woocommerce_get_loop_display_mode() result isn't filterable directly.
But it relies on some values from get_option() (since the setting is in the customizer) and get_option() is filterable via option_$option
So a potential way to tell WooCommerce not to display the categories when you have a particular $_GET parameter, you could filter the shop display mode option to be products when that parameter is detected in the URL. Like this:
/**
* Filter shop display type.
*
* #param string $value - 'products' | 'subcategories' | 'both'
* #return string
*/
function kia_woocommerce_shop_page_display( $value ) {
if ( ! empty( $_GET['param1'] ) {
$value = 'products';
}
return $value;
}
add_filter( 'option_woocommerce_shop_page_display', 'kia_woocommerce_shop_page_display' );
EDIT I: I have found the file where the old plugin Woocommerce Blocks sets the blocks: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/master/src/BlockTypes/FeaturedCategory.php But where is it in the Woocommerce library?
EDIT II: Question in short:
How do you customize the Woocommerce Blocks to show more data than the build in functionality?
------------- background ------------
If you search for adding custom attributes for Woocommerce Blocks you find a lot of WordPress examples for this.
For example, this, where the answer points out, that you can add attributes by using the blocks.registerBlockType. But how to do this for Woocommerce Blocks?
I want to be able to add a data field to the HTML output. The data field should then call a product attribute and show if it exists.
So when you use the Woocommerce Blocks on your front page - for example, the size will be shown underneath the add to cart button - as in the image.
As you might know the functionality of showing/hiding the price, add-to-cart-button, reviews are already there, when you choose a Woocommerce Block on the editing site.
But I haven't found the place where this functionality is created.
This would also be a great help actually - if you could show me where in the Woocommerce Github library the blocks are being created. Maybe I can figure out my self how to filter through them and add the functionality
I know - based on a Udemy course - how to create a custom plugin and create a new blog-type, save and edit.
But I need to figure out what Woocommerce namespace is, how they create their blocks, and what their data is called. The Woocommerce developer handbook is not saying anything about this - not what I've found.
I've been searching the internet for three days now, and I just don't understand that I can't seem to find ANYTHING on this. That nobody else wants to customize this functionality in Woocommerce. I know it is a new function (blocks) in the core, but still.
I just need to be pointed in the right direction.
I was dealing with the exact same problem as you and I found the answer by digging deeply on the WC blocks plugin repo.
I found that you have to apply a filter to this hook: woocommerce_blocks_product_grid_item_html
The original HTML is this:
<li class="wc-block-grid__product">
<a href="{$data->permalink}" class="wc-block-grid__product-link">
{$data->image}
{$data->title}
</a>
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
</li>
So that way you can filter the html code and modify it by adding this chunk of code to your functions.php and customizing it to fit your needs
function wc_add_date_to_gutenberg_block( $html, $data, $product ) {
$dateStr = get_post_meta($product->get_id(), 'ticket_start_time', true);
$date = new DateTime($dateStr);
$data->date = "<p>Date: " . $date->format('d-m-Y H:i') . "</p>";
$output = "
<li class=\"wc-block-grid__product\">
<a href=\"{$data->permalink}\" class=\"wc-block-grid__product-link\">
{$data->image}
{$data->title}
</a>
{$data->date} <- I added this one
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
</li>
";
return $output;
}
add_filter("woocommerce_blocks_product_grid_item_html", "wc_add_date_to_gutenberg_block", 10, 3);
I'm not totally clear on what you're asking. You reference Gutenberg Blocks often, but have linked to a WooCommerce repository that doesn't have any Gutenberg Blocks.
But if I'm understanding you correctly, you're looking for the PHP template that controls products. You can find in content-product.php
You'll see a lot of calls to do_action which is core to WordPress hooks as used in plugin development.
<li <?php wc_product_class( '', $product ); ?>>
<?php
/**
* Hook: woocommerce_before_shop_loop_item.
*
* #hooked woocommerce_template_loop_product_link_open - 10
*/
do_action( 'woocommerce_before_shop_loop_item' );
/**
* Hook: woocommerce_before_shop_loop_item_title.
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
/**
* Hook: woocommerce_shop_loop_item_title.
*
* #hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item_title.
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item.
*
* #hooked woocommerce_template_loop_product_link_close - 5
* #hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
If you do a search for the action hooks defined in content-product.php, you'll find them defined in wc-template-hooks.php. Hooks are named actions that functions are added to. For example if you look into the woocommerce_after_shop_loop_item action, you'll find these two functions being attached to it.
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
The woocommerce_template_loop_product_link_close and woocommerce_template_loop_add_to_cart functions are defined in wc-template-functions.php
You could create an entirely new content-product.php file in your theme by creating a file in yourtheme/woocommerce/content-product.php, however you then lose a lot of the built in power and compatibility of WooCommerce.
Better would be to remove then add new actions to the woocommerce_after_shop_loop_item hook. For example, woocommerce_template_loop_product_link_close is currently defined as:
function woocommerce_template_loop_product_link_close() {
echo '</a>';
}
You could overwrite this by doing this in your functions.php file:
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
function custom_template_loop_product_link_close() {
echo 'Hello world!</a>';
}
add_action( 'woocommerce_after_shop_loop_item', 'custom_template_loop_product_link_close', 5 );
I hope this helps.
I created a template (call it A.php) for a specific set of pages on my website.
How would one retrieve the list of medias (previously) attached to a page whose template is A.php in A.php?
I want to be able to iterate through that list of media in this A.php template. Here is a minimalistic version of A.php:
<?php
/**
* Template Name: test layout
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage jbWeb
* #since 1.0
* #version 1.0
*/
get_header();
query_posts(array('pagename' => get_query_var('pagename')));
while (have_posts()) {
the_post();
ob_start();
the_title();
$title = ob_get_clean();
echo $title;
$medias = get_attached_media( 'image', get_the_ID());
foreach($medias as $media) {
echo ('this is a url: ' . wp_get_attachment_url($media->id));
}
}
get_footer();
?>
apparently the only post that is retrieved from the while(have_posts()) loop is the page itself (or is it the content of the page itself?). This is what is shown on my browser:
The Url of any media attached to the page is never shown. And there are only two medias when I attached four photos to "pagetest1".
Here is what my admin page for the page "pagetest1" looks like:
Well here is a solution I have found which allows for any type of media to be displayed (you just have to tweak a part of the solution I will show how):
<?php
/**
* The template for displaying all pages
* Template Name: test layout
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage jbWeb
* #since 1.0
* #version 1.0
*/
get_header();
//the query to select the post id of the post containing all content of this page.
//have to replace pageTest by get_query_var('pagename')
$page_content = $wpdb->get_var(
$wpdb->prepare("
select post_content from (select max(post_date) as post_date from wp_posts
where post_title='%s' group by post_title) as sub,
wp_posts as p where p.post_date = sub.post_date ;",
get_query_var('pagename'))
);
$matches = array();
preg_match_all('/<img.*\/>/mU', $page_content, $matches, PREG_PATTERN_ORDER);
foreach ($matches as $m) {
foreach ($m as $m1) {
echo $m1 . "\n";
}
}
get_footer();
?>
Explanation:
The mysql query
So first there is that query on the mysql database that is done where I select the content of the most recent version of the post intrinsic to the page whose template is "test layout". I pass get_query_var('pagename') as parameter to this mysql query, this get_query_var function returns the current page name.
the pattern match
Then I find all occurences of the <img/> tag within the content of that post. About the m and U option within the pattern: m signifies that I want the input $page_content to be seen as a single line. And U signifies that I want the regexp to not be greedy, so that I find all tags and not simply one tag that would contain other tags.
printing the result
Finally the double foreach loop prints all <img/> tag within that resulting page.
Note: You could find all occurences of a <video/> tag in the same way. Just replace <img.*\/> within the pattern by <video.*\/>
I want to know how can I add a custom html code in a genesis template between entry-meta and entry-content in a post page.
Thanks.
Use the genesis_entry_header action. This allows you to inject content into the end of the header.
add_action( 'genesis_entry_header', 'add_stuff_after_title', 12 );
function add_stuff_after_title() {
if(is_singular('post')) {
_e("<span>This is before the ending header tag.</span>");
}
}
This the genesis code that close the header
add_action( 'genesis_entry_header', 'genesis_entry_header_markup_close', 15 );
/**
* Echo the closing structural markup for the entry header.
*
* #since 2.0.0
*/
function genesis_entry_header_markup_close() {
echo '</header>';
}
The priority is 15 and that the reason that priority for the my code has to be greater
add_action( 'genesis_entry_header', 'add_stuff_after_title', 100 );
function add_stuff_after_title() {
if(is_singular('post')) {
_e("<span>This is before the ending header tag.</span>");
}
}