Trim title at side bare - wordpress

My wordpress theme has a "save post as a favortite" function. And it marks the post as a favorite at side bar. but it wont shorten the title. the long titles at sidebar looks messy. in function i use:
function short_title( $after = '', $length ) {
$mytitle = get_the_title();
if( mb_strlen( $mytitle ) > $length ) {
$mytitle = mb_substr( $mytitle, 0, $length );
echo $mytitle . $after;
} else echo $mytitle;
}
and i call it with:
<?php short_title( '...', 99 ); ?>
how can i put short_title in to here:
echo '</p>';
echo '<h4>' . stripslashes( strip_tags( $post_obj_fave->post_title ) ) . '</h4>';
echo '<p class="info">';

You need to apply the shorten title function to $post_obj_fave->post_title
So change your code from:
echo '<h4>' . stripslashes( strip_tags( $post_obj_fave->post_title ) ) . '</h4>';
To:
echo '<h4>' . stripslashes( strip_tags( shorten_title($post_obj_fave->post_title, $length=99) ) ) . '</h4>';
Now create a function for shorten_title()
Here it is:
function shorten_title($var, $length ) {
if( mb_strlen( $var ) > $length ) {
$var= mb_substr( $var, 0, $length );
return $var;
} else return $var;
}
This should shorten the passed variable ($post_obj_fave->post_title) in this case according to associated length.

Related

Hide certain acf fields wordpress

Help
Code in template:
<?php
$groupID = '';
$fields = get_fields($groupID);
$fields = get_field_objects();
if( $fields )
{
foreach( $fields as $field_name => $field )
{
if( $field['value'] )
{
echo '<ul>';
echo '<li>' . $field['label'] . ': <strong>' . $field['value'] . '</strong></li>';
echo '</ul>';
}
}
}
?>
I need to hide the fields:
  field_5c0a8d44cf56e
  field_5c0a8d4ecf56f
How can i do this?
Your question is not much clear for me, and from my knowledge what I have understood,
for an acf group you don't want to loop through, the control is yours,
so you can print it straightaway
but if you really want to do it in a loop then,
if( $fields )
{
foreach( $fields as $field_name => $value )
{
if( $value && !in_array($field_name, ["field_5c0a8d44cf56e", "field_5c0a8d4ecf56f"])
{
echo '<ul>';
echo '<li>' . $field_name . ': <strong>' . $value . '</strong></li>';
echo '</ul>';
}
}
}

How to make a change to a file in wp-includes?

I'm using the Hestia theme. I created a child theme for Hestia.
Hestia theme
wp-content/themes/hestia
Child Theme
wp-content/themes/hestia-child
I need to make a change in the file wp-includes/general-template.php
How Can I make the change below without directly altering the file from wp-includes?
wp-content/themes/hestia/archive.php
<?php the_archive_title( '<h1 class="hestia-title">', '</h1>' ); ?>
wp-includes/general-template.php
// This function calls get_the_archive_title
function the_archive_title( $before = '', $after = '' ) {
$title = get_the_archive_title();
if ( ! empty( $title ) ) {
echo $before . $title . $after;
}
}
// Original
function get_the_archive_title() {
if ( is_author() ) {
$title = sprintf( __( 'Author: %s' ), '<span class="vcard">' . get_the_author() . '</span>' );
}
}
// What I need to change - 'Author: %s' to '%s'
function get_the_archive_title() {
if ( is_author() ) {
$title = sprintf( __( '%s' ), '<span class="vcard">' .get_the_author() . '</span>' );
}
}
Some "filter" and "hook" putting this in your functions.php :
Please check below reference link.
Link1 :- https://wordpress.stackexchange.com/questions/60605/function-to-change-a-label-username-in-a-core-wordpress-file-wp-includes-gene/60607?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
Link2 :- https://wordpress.stackexchange.com/questions/255677/how-to-modify-files-inside-wp-includes-directory-in-wordpress?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
add_filter( 'get_the_archive_title', function ( $title ) {
if( is_category() ) {
$title = single_cat_title( '', false );
}
return $title;
});

default thumbnail for all posts in same category

My Wordpress site use first image as post thumbnail, code:
add_filter('the_content','replace_content');
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$first_img = "/path/to/default.png";
}
return $first_img;
}
Some posts have no image in its content, so i want to use different default thumbnails for them: posts in category A use picture1, posts in cagory B use category2...
How can I do this?
thank you
You need to get the current post category first, then, make a if statement in your code.
Check this link : https://developer.wordpress.org/reference/functions/get_the_category/#comment-305
How about this, here we are using has_post_thumbnail to check for image attachments, if none exist we are setting up the image and image source. From there we check for category assignment, if there are no category matches we use a default thumbnail.
<?php
if ( ! has_post_thumbnail() ) {
$themefolder = get_bloginfo('template_directory');
echo '<img src="' . $themefolder . '/images/';
if ( is_category( 'Category A' ) ) {
echo 'no-image-a.png';
} elseif ( is_category( 'Category B' ) ) {
echo 'no-image-b.png';
} else {
echo 'no-image.png';
}
echo '" alt="' . get_the_title() . '">' . PHP_EOL;
}
?>
Finally, It's my code:
add_filter('the_content','replace_content');
function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if(empty($first_img)) {
$categories = get_the_category();
if ( ! empty( $categories ) ) {
foreach ( $categories as $category ) {
if( $category->name == 'Category A' ){ $first_img = "/path/to/default1.png";}
elseif ( $category->name == 'Category B' ){ $first_img = "/path/to/default2.png";}
else {$first_img = "/path/to/default3.png";}
}
}
}
return $first_img;
}

Display sidebar after x paragraphs

I am looking for a way to display a sidebar dynamically after x number of paragraphs in my content.
Problem : dynamic_sidebar (' name ') doesn't display text : var_dump($ad_code) = bool(true).
Result : My sidebar is displayed twice in the header, once before the content and at the right paragraphs it displays the number "1".
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = dynamic_sidebar( 'sidebar-6' );
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
I just add this :
ob_start();
dynamic_sidebar('sidebar-id');
$sidebar = ob_get_contents();
ob_end_clean();
It's working !

Display Custom Post Type Metabox Data

I'm having a problem displaying metabox data in a page template wp_query. I get this error:
Notice: Array to string conversion
Here is my code:
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'portfolio' ),
'ignore_sticky_posts' => true,
'posts_per_page' => '10',
);
// The Query
$portfolio_query = new WP_Query( $args );
// The Loop
if ( $portfolio_query->have_posts() ) {
while ( $portfolio_query->have_posts() ) {
$portfolio_query->the_post();
echo '<div id="portfolio">';
echo '<div class="featured_img">';
echo '' . " " . get_the_post_thumbnail() . '';
echo '</div>';
echo '<div class="portfolio">';
echo the_title( '<h2>', '</h2>' );
echo '<p>' . the_excerpt() . '</p>';
echo '<p>' . get_post_meta( get_the_ID($post->id , 'project_metabox', false) ) . '</p>';
echo '</div>';
echo '</div>';
}
} else {
echo "<h1>There are no portfolio pieces to view.</h1>";
}
// Restore original Post Data
wp_reset_postdata(); ?>
I've tried everything. This is just the latest version of my attempts. What am I doing wrong?
UPDATE:
Alright, so I found out the metadata isn't saving. I'm not exactly sure why, but I tried fixing it, and it's not working. My fixes made it worse. Here is my code:
`
public function save_metabox( $post_id, $post ) {
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Sanitize user input.
$project_new_web_design = isset( $_POST[ 'project_web_design' ] ) ? 'checked' : '';
$project_new_web_development = isset( $_POST[ 'project_web_development' ] ) ? 'checked' : '';
$project_new_digital_art = isset( $_POST[ 'project_digital_art' ] ) ? 'checked' : '';
$project_new_graphic_design = isset( $_POST[ 'project_graphic_design' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'project_web_design ', $project_new_web_design );
update_post_meta( $post_id, 'project_web_development ', $project_new_web_development );
update_post_meta( $post_id, 'project_digital_art ', $project_new_digital_art );
update_post_meta( $post_id, 'project_graphic_design ', $project_new_graphic_design );
}
}
I believe the issue you're having is on this line:
echo '<p>' . get_post_meta( get_the_ID($post->id , 'project_metabox', false) ) . '</p>';
You seem to have mixed get_the_ID and $post->ID together. You're also attempting to echo an array.
The third parameter of get_post_meta will determine whether the value returned is a string or an array. Use true to return a single value.
Change it to:
echo '<p>' . get_post_meta( get_the_ID(), 'project_metabox', true ) . '</p>';
On a minor note you're trying to echo functions like the_excerpt(). You'll typically find that functions starting the_ instead of get_the_ will output directly instead of returning a value. Furthermore the_excerpt() will automatically add paragraph tags.
Replace this:
echo '<p>' . the_excerpt() . '</p>';
With this:
the_excerpt();

Resources