I need chapter numbers in a WordPress Loop - php

I tried to solve something but struggling the whole morning.
I have my WordPress posts already in the state that they can have a parent post.
done by:
add_action('init', 'add_hierarchy_to_posts');
function add_hierarchy_to_posts() {
add_post_type_support( 'post', 'page-attributes' );
$obj = get_post_type_object( 'post' );
$obj->hierarchical =true;
remove_theme_support( 'post-formats' );
}
I need my post numerized in chapters so thats
1. Introduction
2. Subject is great
2.1 why great
2.2 why subject
2.2.1 better
2.2.2 worse
2.2.2.1 in best case
3. End
The problem is now, how to count subchapters.
The function is here so far, and it produces at least1.02.03.03.13.2
global $chapter,$subchapter,$subchapter1,$subchapter2,$depper;
function is_child_of($postID) {
global $post;
$ico=$post->post_parent;
return $ico;
}
function find_out_chapter_number($id){
global $chapter,$subchapter,$subchapter1,$subchapter2,$subchapter3,$depper;
if(is_child_of($id)==0){
$chapter++;
$subchapter=0;
}
else if(is_child_of($id)==$depper){
$subchapter++;
}
$depper=$id;
$ico=is_child_of($id);
//$id. " - ".$ico." - ".
return $chapter.".".$subchapter.".".$subchapter1.".".$subchapter2;
}
The $depper variable stores the post id before.
I need it deeper and I thought its maybe something with recursion but I don't know.
The function is called from the wordpress loop with$kapitel = find_out_chapter_number(get_the_ID()); and echoed before the_title();

Please try this plugin... this will help.
Page List plugin
if you still facing the same problem. please leave your comment. i will give you another solution.

Related

How to allow/call multiple post types in wordpress

I am a newbie in coding. Can you help me this code that I got from jetpack.
function allow_my_post_types($allowed_post_types) {
$allowed_post_types[] = ‘wpdmpro’;
return $allowed_post_types;
}
add_filter( ‘rest_api_allowed_post_types’, ‘allow_my_post_types’ );
that code above was working perfectly. I allowed 'wpdmpro'
I want to call multiple post types. I want to include 'dwqa-question' and 'topic'. Please help me if I got wrong with the code that I just created because its not working after added those two post types.
function allow_my_post_types($allowed_post_types) {
$allowed_post_types[] = ‘wpdmpro’,’topic’,’dwqa-question’;
return $allowed_post_types;
}
add_filter( ‘rest_api_allowed_post_types’, ‘allow_my_post_types’ );
thank you so much for those who can help. I am just learning by myself. Thanks.
Try to add the values one by one to the array like this:
$allowed_post_types[] = ‘wpdmpro’;
$allowed_post_types[] = ’topic’;
$allowed_post_types[] = ’dwqa-question’;
Or you may find some useful solution here:
PHP Arrays
Try this
function allow_my_post_types($allowed_post_types) {
array_push($allowed_post_types,'wpdmpro','topic','dwqa-question');
return $allowed_post_types;
}
add_filter( ‘rest_api_allowed_post_types’, ‘allow_my_post_types’ );

Working with Function have_posts from WordPress

I'm working with the function have_posts from WordPress codex. What I'm trying to do is:
while ($content_query->have_posts()) {
$content_query->the_post();
if(strpos(the_title(),'Garajes Gran')===false){
global $post;
include($item_template);
}
}
So I get a full list of names with Garajes Gran names included and not the list without the name Garajes Gran, What I'm doing wrong?. Also instead of look for the first position where the String Garajes Gran it's, it prints all names, so why if(strpos(the_title(),'Garajes Gran')===false){ doesn't do what it means?.
Thanks in advance.
Try the below code used 'get_the_title()' instead of 'the_title()'.
if(strpos(get_the_title( get_the_ID() ),'Garajes Gran')===false){
global $post;
include($item_template);
}
I hope this is working for you.

how to write a script output in a post in the act of creation of these posts?

Hello community WordPress forum.
I have a need to write the posts the output of a function, but all to getting to do is to display real-time and run this script / function in all posts, with I do to write the posts the output of a function instead of display it in posts?
because what 's happening is that the script is running on all posts, and each refresh / access the pages, a new script number is generated! I would like the generator create a different number for each post, but write to output them, and not display a number to each new access.
// Declare the function
function gen_num()
{
// DETERMINE THE CHARACTER THAT CONTAIN THE PASSWORD
$caracteres = "012345678910111213141516171819";
// Shuffles THE CHARACTER AND HANDLE ONLY THE FIRST 10
$mistura = substr(str_shuffle($caracteres),0,10);
// DISPLAYS THE OUTCOME
print $mistura;
}
// Add custom post content, inject function in content.
function add_post_content($content) {
gen_num();
return $content;
}
add_filter('the_content', 'add_post_content');
see in herculestest.tk, browse the pages, make f5 to refresh.
Thank you very much.
==========================================
another attempt:
I created a custom field from ACF plugin named: numeration:
function gen_num()
{
global $post;
$mistura = get_post_meta( $post->ID, 'numeration', true );
if ( '' == $mistura ) {
//DETERMINE THE CHARACTER THAT CONTAIN THE PASSWORD
$caracteres = "012345678910111213141516171819";
// Shuffles THE CHARACTER AND HANDLE ONLY THE FIRST 10
$mistura = substr(str_shuffle($caracteres),0,10);
update_post_meta( $post->ID, 'numeration', $mistura );
}
//DISPLAYS THE OUTCOME
print $mistura;
}
// Add custom post content, inject function in content.
function add_post_content($content) {
gen_num();
return $content;
}
add_filter('the_content', 'add_post_content');
this solution to not change the number each new access, now writing these data permanently in the database ?? because're not recording! if I change my theme, all the numbers in all the posts disappear, and if I make any errors in functions, php, these add up numbers, because they depend on the function running at him display the values, and worst of all, to fix the functions.php, the script will return to run and therefore, will be a re-run, which means that it will generate new numbers on all posts !! and this can not happen, should I ever have the same values!
The Post ID is unique in WordPress, is that number adequate for your needs?
Otherwise you'd need to generate a unique ID when the post is published and save it in a custom field. (Instead of your gener_numb function look into uniqid: http://php.net/manual/en/function.uniqid.php.)

Adding a Secondary Root URL to WordPress Rewrites

I'm currently working on a project that has requirements of having multiple paths to the same contents. The project will have 4 different brands, for the sake of argument, we'll call those brand1, brand2, brand3, and brand4.
The desire is to generate rewrite rules for WordPress so that all of the following would work:
http://primaryurl.com/page-1 (Brand 1 is the base URL)
http://primaryurl.com/brand2/page-1
http://primaryurl.com/brand3/page-1
http://primaryurl.com/brand4/page-2
This would also be true of any custom post types that exists, posts, etc.
I've dug into WP_Rewrite a bit but haven't quite figured out how to solve this challenge and feel like I might just be missing something.
I believe this could be accomplished via a ton of add_rewrite_rule() calls but feel like there has to be a better way.
Thanks in advance for any advice on how to tackle this.
I wound up solving this with the following:
$brands = array( 'brand2','brand3','brand4' );
add_action( 'generate_rewrite_rules', function( $wp_rewrite ) {
global $brands;
$new_rewrite_rules = array();
foreach ($brands as $brand) {
foreach ($wp_rewrite->rules as $regex => $redirect) {
$new_rewrite_rules[$brand . '/' . $regex] = $redirect;
}
}
//All rewrite rules are expected to be set at this moment
$wp_rewrite->rules = array_merge($new_rewrite_rules, $wp_rewrite->rules);
} );

Wordpress Plugin Using Permalinks

So i created a plugin that is basically a gallery that you choose options as you go.
First choose Brand
Then Color
Then Style
At each step i am passing the variables via $_GET
So once you have chosen your brand and continue the next page URL is cabinets/?brand=1
Then after you choose your color it is cabinets/?brand=1&color=2
i have written a rewrite for this that is supposed to make pretty urls but all it is doing is showing the home page.
add_filter('rewrite_rules_array','cabinets_rewrite_rules_array');
function cabinets_rewrite_rules_array($rules){
$cabinets_slug = 'cabinets';
$my_cab_rules[$cabinets_slug.'/?$'] = $cabinets_slug."/?brand=$matches[1]";
$my_cab_rules[$cabinets_slug.'/(.+?)/?$'] = $cabinets_slug."/?brand=$matches[1]&color=$matches[2]";
$my_cab_rules[$cabinets_slug.'/(.+?)/(.+?)/?$'] = $cabinets_slug."/?brand=$matches[1]&color=$matches[2]&style=$matches[3]";
return $my_cab_rules + $rules;
}
i have tried many things even as much as updating the htaccess file but i dont want to have to do that since this is a plugin.
Any Idea?
You could try adding your new variables to the list of query vars.
function prfx_add_query_vars($aVars) {
global $wp_query;
$aVars[] = "brand";
$aVars[] = "color";
$aVars[] = "style";
return $aVars;
}
add_filter('query_vars', 'prfx_add_query_vars');

Categories